package org.softmed.neodatis.util;

import java.math.BigInteger;

import org.neodatis.odb.ClassRepresentation;
import org.neodatis.odb.ODB;
import org.neodatis.odb.ODBExt;
import org.neodatis.odb.OID;
import org.neodatis.odb.Objects;
import org.neodatis.odb.Values;
import org.neodatis.odb.core.layers.layer3.IRefactorManager;
import org.neodatis.odb.core.query.IQuery;
import org.neodatis.odb.core.query.IValuesQuery;
import org.neodatis.odb.core.trigger.DeleteTrigger;
import org.neodatis.odb.core.trigger.InsertTrigger;
import org.neodatis.odb.core.trigger.SelectTrigger;
import org.neodatis.odb.core.trigger.UpdateTrigger;
import org.neodatis.odb.impl.core.query.criteria.CriteriaQuery;
import org.softmed.neodatis.util.id.ClassFieldIDGenerators;
import org.softmed.neodatis.util.id.GenerateIDTrigger;

public class FakeODB implements ODB {

	GenerateIDTrigger insert;
	ODB odb;

	public FakeODB(ODB odbm, GenerateIDTrigger insert) {
		this.odb = odbm;
		this.insert = insert;
	}

	public FakeODB() {

	}

	@Override
	public OID store(Object object) {
		if (object.getClass() == this.getClass()
				|| object.getClass() == ClassFieldIDGenerators.class)
			return odb.store(object);
		OID oid = null;
		try {
			insert.getStoredGenerators(odb);
			insert.beforeInsert(object);
			oid = odb.store(object);
			insert.storeGenerators(odb);
		} catch (Throwable e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}

		return oid;
	}

	@Override
	public void close() {
		odb.close();
	}

	@Override
	public void commit() {
		odb.commit();
	}

	@Override
	public BigInteger count(CriteriaQuery query) {
		return odb.count(query);
	}

	@Override
	public void defragmentTo(String newFileName) {
		odb.defragmentTo(newFileName);
	}

	@Override
	public OID delete(Object object) {
		return odb.delete(object);
	}

	@Override
	public void deleteObjectWithId(OID oid) {
		odb.deleteObjectWithId(oid);

	}

	@Override
	public ODBExt ext() {
		return odb.ext();
	}

	@Override
	public ClassRepresentation getClassRepresentation(Class clazz) {
		return odb.getClassRepresentation(clazz);
	}

	@Override
	public ClassRepresentation getClassRepresentation(String fullClassName) {
		return odb.getClassRepresentation(fullClassName);
	}

	@Override
	public Object getObjectFromId(OID id) {
		return odb.getObjectFromId(id);
	}

	@Override
	public OID getObjectId(Object object) {
		return odb.getObjectId(object);
	}

	@Override
	public Objects getObjects(Class clazz) {
		return odb.getObjects(clazz);
	}

	@Override
	public Objects getObjects(IQuery query) {
		return odb.getObjects(query);
	}

	@Override
	public Objects getObjects(Class clazz, boolean inMemory) {
		return odb.getObjects(clazz, inMemory);
	}

	@Override
	public Objects getObjects(IQuery query, boolean inMemory) {
		return odb.getObjects(query, inMemory);
	}

	@Override
	public Objects getObjects(Class clazz, boolean inMemory, int startIndex,
			int endIndex) {
		return odb.getObjects(clazz, inMemory, startIndex, endIndex);
	}

	@Override
	public Objects getObjects(IQuery query, boolean inMemory, int startIndex,
			int endIndex) {
		return odb.getObjects(query, inMemory, startIndex, endIndex);

	}

	@Override
	public IRefactorManager getRefactorManager() {
		return odb.getRefactorManager();
	}

	@Override
	public Values getValues(IValuesQuery query) {
		return odb.getValues(query);
	}

	@Override
	public void rollback() {
		odb.rollback();

	}

	public ODB getOdb() {
		return odb;
	}

	public void setOdb(ODB odb) {
		this.odb = odb;
	}

	@Override
	public void addDeleteTrigger(Class clazz, DeleteTrigger trigger) {
		// TODO Auto-generated method stub

	}

	@Override
	public void addInsertTrigger(Class clazz, InsertTrigger trigger) {
		// TODO Auto-generated method stub

	}

	@Override
	public void addSelectTrigger(Class clazz, SelectTrigger trigger) {
		// TODO Auto-generated method stub

	}

	@Override
	public void addUpdateTrigger(Class clazz, UpdateTrigger trigger) {
		// TODO Auto-generated method stub

	}

	@Override
	public void disconnect(Object object) {
		odb.disconnect(object);
	}

	@Override
	public void reconnect(Object object) {
		odb.reconnect(object);
	}

	public GenerateIDTrigger getInsert() {
		return insert;
	}

}
